import pymysql
import schedule
import time
import urllib.request
import ssl
class Opreation:
    def __init__(self):
        self.host = 'localhost'
        self.uNmae = 'root'     # 数据库连接用户名
        self.pwd = 'root'       # 数据库连接密码
        self.db = 'one'         # 数据库名
        self.charset = 'utf8'
        self.sql = 'SELECT * FROM hitikoto ORDER BY RAND() LIMIT 1'
        
        
    def query(self):
        # 查询数据
        conn = pymysql.connect(host = self.host, user = self.uNmae, passwd = self.pwd, db = self.db, charset = self.charset)
        cur = conn.cursor()
        cur.execute(self.sql)
        data = cur.fetchone()
        cur.close()
        conn.close()
        if(data[2] is None):
            content = data[1]
        else:
            content = data[1] + "\t" + "——" + data[2]
        print(content)      # print用于测试，不需要可删除
        
        #请求接口、发送通知
        contentUnicode = urllib.parse.quote(content)
        ssl._create_default_https_context = ssl._create_unverified_context
        url = "https://api2.pushdeer.com/message/push?pushkey=你的手机端密钥"+contentUnicode
        
        head = {
            # 用户代理，告诉服务器我们是什么类型的继器、浏览器，本质上时告诉浏览器我们可以接受什么水平的页面内容
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
        }
        
        request = urllib.request.Request(url, headers=head)
        response = urllib.request.urlopen(request,timeout=1200)
        print(response.read())
        

    


if __name__ == "__main__":
    op = Opreation()
    # schedule.every(0.1).minutes.do(op.query) 每隔0.1分调用，用于测试
    schedule.every().day.at("08:00").do(op.query) # 每天8点执行，按需更改
    while True:
        schedule.run_pending()
        time.sleep(1)